home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / New System Software Extensions / Thread Manager Extension 2.0.1 / Sample Applications / Power Examples / SortPicts / Source / ShellSort.cp < prev    next >
Encoding:
Text File  |  1994-06-03  |  1.9 KB  |  87 lines  |  [TEXT/MPS ]

  1. /*************************************************************************************
  2.  *
  3.  *    Apply a ShellSort algorithm to an offscreen, scrambled GWorld
  4.  *
  5.  *    ShellSort.cp    -    C Source
  6.  *
  7.  *      Copyright © Apple Computer, Inc. 1988 - 1993
  8.  *      All rights reserved.
  9.  *
  10.  *    This is the "guts" of the SortPicts program.  This file performs the actual
  11.  *      sort algorithm which gives the image its restoration quality (restoring itself
  12.  *       to its original self).
  13.  *
  14.  *************************************************************************************/
  15. #include "GWorldObj.h"
  16.  
  17.  
  18.  
  19. /*************************************************************/
  20. /*                                                           */
  21. /*               The Actual Sort Algorithms                  */
  22. /*                                                           */
  23. /*************************************************************/
  24.  
  25.  
  26. void    GWorldObj :: ShellSort( void)
  27. {
  28.     long            loop, sortSize, inLoop;
  29.     long            min;
  30.     long            data;
  31.     long           *tickPtr = (long *)0x16A;
  32.     char            mmuMode = true32b;
  33.     
  34.     SetTitle( (const unsigned char*)"\pShell Sorting");
  35.  
  36.     SwapMMUMode( &mmuMode);
  37.     UseGWorld();
  38.     
  39.     HLock( (Handle)sortList);
  40.     sortListPtr = *sortList;
  41.     if( !sortListPtr)
  42.         return;
  43.     
  44.     sortSize = 1;
  45.     while( sortSize < N)
  46.         sortSize = 3 * sortSize + 1;
  47.  
  48.     do
  49.     {
  50.         sortSize /= 3;
  51.         for( loop = sortSize; loop < N; ++loop)
  52.         {
  53.  
  54.             min = sortListPtr[loop];
  55.  
  56.             inLoop = loop;
  57.             while( (data = sortListPtr[inLoop - sortSize]) > min)
  58.             {
  59.                 SetSortItem( inLoop, data);
  60.                 
  61.                 inLoop -= sortSize;
  62.                 if( inLoop < sortSize)
  63.                     goto Next;
  64.                 else
  65.                     if( YieldIfTime( &mmuMode))
  66.                         goto SortErrQuit;
  67.             }
  68. Next:    
  69.             SetSortItem( inLoop, min);
  70.         }            
  71.     } while( sortSize > (gSortSize * 4));            //  1 = cool picts; 0 = actual sort
  72.  
  73.     UnuseGWorld();
  74.     SwapMMUMode( &mmuMode);
  75.  
  76. SortErrQuit:
  77.     HUnlock( (Handle)sortList);
  78.     SetTitle( (const unsigned char*)"\pDone");
  79.     
  80.     doneSorting = true;
  81.     finishedTime = TickCount();
  82.     DrawObj();
  83. }
  84.  
  85.  
  86.  
  87.